package com.example.shiwuyouproject.ui.fragment

import android.view.LayoutInflater
import com.example.shiwuyouproject.R
import com.example.shiwuyouproject.base.BaseVmFragment
import com.example.shiwuyouproject.databinding.FragmentMessageBinding
import com.example.shiwuyouproject.ui.bean.GetMessageListsBean
import com.example.shiwuyouproject.ui.fragment.home.bean.HomeRecommendListDataBean
import com.example.shiwuyouproject.ui.fragment.message.adapter.MessageAdapter
import com.example.shiwuyouproject.ui.modelview.MessageViewModel
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener

class MessageFragment:BaseVmFragment<FragmentMessageBinding, MessageViewModel>() {
    override fun viewModelClass(): Class<MessageViewModel> =MessageViewModel::class.java
    override fun getViewBinding(): FragmentMessageBinding {
        return FragmentMessageBinding.inflate(layoutInflater)
    }

    override fun initView() {

    }
    private var adapter: MessageAdapter? = null
    override fun initData() {
        adapter = MessageAdapter()
        binding?.myMessageRecyclerView?.adapter = adapter
        val mview = LayoutInflater.from(context).inflate(R.layout.view_nodata, null)
        adapter?.emptyView = mview
        mBinding.refreshLayout.autoRefresh()
    }
    private var page=1
    override fun setListener() {
    mBinding.refreshLayout.setOnRefreshLoadMoreListener(object : OnRefreshLoadMoreListener {
        override fun onRefresh(refreshLayout: RefreshLayout) {
            page=1
            mViewModel.getMessageLists(page.toString())
        }

        override fun onLoadMore(refreshLayout: RefreshLayout) {
            page++
            mViewModel.getMessageLists(page.toString())
        }
    })
        adapter?.setOnItemClickListener { adapter, view, position ->
           var  item:GetMessageListsBean= adapter?.getItem(position) as GetMessageListsBean
              when(item.action_type){
                  "0"->{// 无
//                      action_data  空
                  }
                  "1"->{//进入工作台
//                      action_data  空
                  }
                  "2"->{//进入链接
//                      action_data  链接
                  }
                  "3"->{//进入招聘详情
//                      action_data  招聘ID
                  }
              }

        }
    }
    override fun observe() {
        mViewModel.getMessageListsStatus.observe(this, androidx.lifecycle.Observer<MutableList<GetMessageListsBean>>{
            if(page==1){
                adapter?.setNewData(it)
                mBinding.refreshLayout.finishRefresh()
            }else{
                adapter?.addData(it)
                mBinding.refreshLayout.finishLoadMore()
            }
            if(it.size==0){
                mBinding.refreshLayout.finishLoadMoreWithNoMoreData()
            }
        })

    }
}